package com.dhwl.hsh.merchant.ui.dialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.dhwl.hsh.merchant.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-28 20:50
 */
public class AlertDialogFragment extends DialogFragment {

    @Bind(R.id.dialog_title_tv)
    TextView tvTitle;
    @Bind(R.id.dialog_content_tv)
    TextView tvContent;
    @Bind(R.id.dialog_btn_cancel)
    Button dialogBtnCancel;
    @Bind(R.id.dialog_btn_sure)
    Button dialogBtnSure;

    String title;
    String content;

    OnSureClickListener onSureClickListener;

    public static AlertDialogFragment newInstance(String title, String content) {

        AlertDialogFragment alertDialogFragment = new AlertDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("content", content);
        alertDialogFragment.setArguments(bundle);
        return alertDialogFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(android.support.v4.app.DialogFragment.STYLE_NORMAL, R.style.loading_dialog);
        title = getArguments().getString("title");
        content = getArguments().getString("content");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.dialog_common_alert, container, false);
        ButterKnife.bind(this, v);
        getDialog().setCanceledOnTouchOutside(false);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setText("提示");
        } else {
            tvTitle.setText(title);
        }
        if (TextUtils.isEmpty(content)) {
            tvContent.setVisibility(View.GONE);
        } else {
            tvContent.setVisibility(View.VISIBLE);
            tvContent.setText(content);
        }
        return v;

    }

    @OnClick(R.id.dialog_btn_sure)
    public void onSureClick(View view) {
        onSureClickListener.onDialogSureClick();
        dismissAllowingStateLoss();
    }

    @OnClick(R.id.dialog_btn_cancel)
    public void onCancelClick(View view) {
        dismissAllowingStateLoss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public OnSureClickListener getOnDialogSureClick() {
        return onSureClickListener;
    }

    public void setOnDialogSureClick(OnSureClickListener onDialogSureClick) {
        this.onSureClickListener = onDialogSureClick;
    }

    public interface OnSureClickListener {
        void onDialogSureClick();
    }
}
